
export const request = (options) => {

	return new Promise((resolve, reject) => {
		let token = uni.getStorageSync('token')
		options.header.Authorization = token
		const { dataType } = options
		options.header = {
			'content-type': dataType == 'json' || !dataType ? 'application/x-www-form-urlencoded' : '',
		}
		delete options.dataType
		uni.request({
			url: process.env.VUE_APP_API_URL + options.url,
			data: options.data || {},
			method: options.method || 'POST',
			header: options.header,
			success: (res) => {
				if (res.data.code == 401) { //刷新token
					if (uni.getStorageSync("userData") == null || uni.getStorageSync("userData") == "") { //没有用户登录信息
						uni.reLaunch({
							//保留当前页面，跳转到应用内的某个页面
							url: '/pages/login/login'
						})
					} else {
						uni.request({
							url: process.env.VUE_APP_API_URL + '/api/login/login',
							data: {
								username:"123456",
								password:"123456"
							},
							header: {
								'content-type': 'application/x-www-form-urlencoded',
							},
							method: 'post',
							success: (res) => {
								if (res.data.code == 200) {
									uni.setStorageSync("token", res.data.msg)

									//重新之前的发送请求
									options.header.Authorization = uni.getStorageSync('token')
									uni.request({
										url: process.env.VUE_APP_API_URL + options.url,
										data: options.data || {},
										method: options.method || 'POST',
										header: options.header,
										success: (res) => {
											resolve(res.data)
										}
									})
								}
							}
						})
					}
				} else {
					// console.log(res.data); // 控制台显示数据信息
					resolve(res.data)
				}

			},
			fail: (err) => {
				// 页面中弹框显示失败

				uni.showToast({
					title: "请检查网络连接",
					icon: 'none',
					duration: 1000
				})
				// 返回错误消息
				reject(err)
			},
			catch: (e) => {
				console.log(e);
			}
		})
	})
}
// 将对象导出外部引入使用
export default {
	request
}